package com.awei.study.controller;


import com.awei.study.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

@RestController
@Slf4j
public class UploadController {

    private static String filepath = "d:\\images\\";
    @RequestMapping("/upload")
    public R upload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(file);
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        String fileUrl = filepath + filename;
        System.out.println(fileUrl);
        try {
            file.transferTo(new File(fileUrl));
        }catch (Exception e) {
            log.error("上传失败",e);
            return R.exception("上传失败");
        }
        return R.ok(fileUrl,"上传成功");
    }
}
